//
//  BH_UnderLineTextField.m
//  Coin
//
//  Created by 杨洋 on 2018/6/10.
//  Copyright © 2018年 zzg. All rights reserved.
//

#import "BH_UnderLineTextField.h"


@interface BH_UnderLineTextField()<UITextFieldDelegate>
@property (nonatomic, strong) UILabel *errorLabel;
@property (nonatomic, strong) UIButton *rightButton;
@end

@implementation BH_UnderLineTextField

#define kTextMarinBottom 15
#define kErrLabelHeight 11

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        self.borderStyle = UITextBorderStyleNone;
        self.lineColor = kColorWithHex(0xDDDFE2);
        self.lineHeight = 1;
        self.delegate = self;
        
        _errorLabel = [[UILabel alloc] init];
        _errorLabel.font = kFont(11);
        _errorLabel.textColor = kColorWithHex(0xF5174D);
        _errorLabel.frame = CGRectMake(0, self.height - _lineHeight - kErrLabelHeight, self.width, kErrLabelHeight);
        
        [self addSubview:_errorLabel];
        
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.frame = CGRectMake(0, 0, 88, 44);
        _rightButton.titleLabel.textAlignment = NSTextAlignmentRight;
        _rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_rightButton addTarget:self action:@selector(buttonHandler) forControlEvents:UIControlEventTouchUpInside];
        self.rightView = _rightButton;
    }
    return self;
}

- (void) buttonHandler
{
    if(_rightHanlder){
        _rightHanlder();
    }
}

- (CGRect)textRectForBounds:(CGRect)bounds
{
    return CGRectMake(self.leftView? 100 : 0, 0, bounds.size.width, bounds.size.height - kTextMarinBottom - kErrLabelHeight);
}

- (CGRect)placeholderRectForBounds:(CGRect)bounds
{
    return CGRectMake(0, 0, bounds.size.width, bounds.size.height - kTextMarinBottom - kErrLabelHeight);
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, self.lineColor.CGColor);
    CGContextFillRect(context, CGRectMake(0, CGRectGetHeight(self.frame)-self.lineHeight - kErrLabelHeight - 5, CGRectGetWidth(self.frame), self.lineHeight));
}
- (CGRect)clearButtonRectForBounds:(CGRect)bounds
{
    CGRect rect = [super clearButtonRectForBounds:bounds];
    CGRect textRect = [self textRectForBounds:bounds];
    return CGRectMake(rect.origin.x, textRect.origin.y, rect.size.width, rect.size.height);
}

- (CGRect)rightViewRectForBounds:(CGRect)bounds
{
    CGRect rect = [super rightViewRectForBounds:bounds];
    return CGRectMake(self.width - rect.size.width, (self.height - kTextMarinBottom - kErrLabelHeight - rect.size.height) / 2.0f, rect.size.width, rect.size.height);
}
- (CGRect)leftViewRectForBounds:(CGRect)bounds
{
    CGRect rect = [super rightViewRectForBounds:bounds];
    return CGRectMake(0, (self.height - kTextMarinBottom - kErrLabelHeight - rect.size.height) / 2.0f, rect.size.width, rect.size.height);
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSString *text = self.text;
    self.lineColor = kColorWithHex(0x3291EB);
    [self setNeedsDisplay];
    self.text = text;
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.lineColor = kColorWithHex(0xDDDFE2);
    [self setNeedsDisplay];
}

- (void)setError:(NSString *)error
{
    _error = error;
    _errorLabel.text = error;
}

- (void)setRightText:(NSString *)rightText
{
    _rightText = rightText;
    [_rightButton setTitle:rightText forState:UIControlStateNormal];
}

- (void)setRightImage:(UIImage *)rightImage
{
    _rightImage = rightImage;
    [_rightButton setImage:rightImage forState:UIControlStateNormal];
}

- (void)setRightTextColor:(UIColor *)rightTextColor
{
    _rightTextColor = rightTextColor;
    [_rightButton setTitleColor:rightTextColor forState:UIControlStateNormal];
}

- (void)setRightTextFont:(UIFont *)rightTextFont
{
    _rightTextFont = rightTextFont;
    _rightButton.titleLabel.font = rightTextFont;
}

- (void)setErrorTextColor:(UIColor *)errorTextColor
{
    _errorTextColor = errorTextColor;
    _errorLabel.textColor = errorTextColor;
}

- (void)setPlaceHolderColor:(UIColor *)placeHolderColor
{
    _placeHolderColor = placeHolderColor;
    [self setValue:placeHolderColor forKeyPath:@"_placeholderLabel.textColor"];
}

- (void)setRightHanlder:(void (^)(void))rightHanlder
{
    _rightHanlder = rightHanlder;
}


- (void) setLeftText:(NSString*) text font:(UIFont*) font color:(UIColor*) color
{

    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 100, 44);
    leftButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    leftButton.titleLabel.font = font;
    [leftButton setTitle:text forState:UIControlStateNormal];
    [leftButton setTitleColor:color forState:UIControlStateNormal];
    leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.leftView = leftButton;
    self.leftViewMode = UITextFieldViewModeAlways;
}
@end
